package com.neusoft.servelt;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

/**
 * @Author: duan
 * @Date: Create in 11:19 2020/10/19
 */
@WebServlet("/request")
public class HttpServletRequestServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost");
        System.out.println("getHeader" + request.getHeader("User-Agent"));
        System.out.println("getParameter" + request.getParameter("account"));//获取参数，不管是get请求的参数还是post请求的参数
        System.out.println("getRequestURI" + request.getRequestURI());
        System.out.println("getRequestURL" + request.getRequestURL());
        System.out.println("getContextPath" + request.getContextPath());
        System.out.println("getAuthType" + request.getAuthType());
        System.out.println("getHeaderNames" + request.getHeaderNames());
        System.out.println("getMethod" + request.getMethod());
        System.out.println("getPathInfo" + request.getPathInfo());
        System.out.println("getPathTranslated" + request.getPathTranslated());
        System.out.println("getParameterMap" + request.getParameterMap().keySet());
        System.out.println("getParameterNames" + request.getParameterNames());
        System.out.println("getParameterValues" + Arrays.toString(request.getParameterValues("account")));
        System.out.println("getRemoteUser" + request.getRemoteUser());
        System.out.println("getRemoteHost" + request.getRemoteHost());//获取客户端的地址
        System.out.println("getRemoteAddr" + request.getRemoteAddr());//获取客户端的地址
        System.out.println("getRemotePort" + request.getRemotePort());
        System.out.println("getServletPath:::" + request.getServletPath());
        System.out.println("getQueryString" + request.getQueryString());

        response.setContentType("text/html;charset=UTF-8");//设置响应的类型
        PrintWriter writer = response.getWriter();//获取流对象
        writer.println("post success 终于成功了");//在页面输出内容

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("getParameter" + req.getParameter("username"));
        System.out.println("getParameter" + req.getParameter("age"));
        System.out.println("getParameterMap" + req.getParameterMap().keySet());
        resp.setContentType("text/html;charset=UTF-8");//设置响应的类型
        PrintWriter writer = resp.getWriter();//获取流对象
        writer.println("get success 终于成功了");//在页面输出内容
    }
}
